@extends('layouts.admin.application')

@section('content')
    <div class="main-content">
        <div class="col-xs-12">
            <div class="breadcrumbs" id="breadcrumbs">
                <ul class="breadcrumb">
                    <li><i class="icon-home home-icon"></i><a href="#">首页</a></li>
                    <li>广告管理</li>
                </ul>
            </div>

            @include('layouts.partials._flash')

            <div class="table-responsive">
                <div id="sample-table-2_wrapper" class="dataTables_wrapper" role="grid">
                    <div class="row">
                        <div class="col-sm-2">
                            <div id="sample-table-2_length" class="dataTables_length">
                                <a href="{{ route('ads.ad.create') }}"
                                   class="btn btn-sm btn-primary">新增</a>
                                <a href="javascript:void(0);" class="btn btn-sm btn-purple del_all">删除</a>
                            </div>
                        </div>

                        <form>
                            <div class="col-sm-10">
                                <div class="dataTables_filter" id="sample-table-2_filter">

                                    <label>
                                        <select class="dataTables_filter" name="category_id" id="change_category"
                                                style="width: 180px;">
                                            <option value="-1">所有分类</option>
                                            @foreach($categories as $category)
                                                <option value="{{$category->id}}"
                                                        @if($category->id == Request::input('category_id')) selected @endif>
                                                    {{$category->name}}
                                                </option>
                                            @endforeach
                                        </select>
                                    </label>
                                    <label>
                                        <a href="{{route('ads.ad.index')}}" class="btn btn-xs btn-default">重置</a>
                                    </label>

                                </div>
                            </div>
                        </form>
                    </div>
                    <table id="sample-table-2"
                           class="table table-striped table-bordered table-hover dataTable del-table"
                           aria-describedby="sample-table-2_info">
                        <thead>
                        <tr role="row">
                            <th class="center sorting_disabled" role="columnheader" rowspan="1" colspan="1"
                                aria-label="">
                                <label>
                                    <input type="checkbox" class="ace" id="check_all">
                                    <span class="lbl"></span>
                                </label>
                            </th>
                            <th>编号</th>
                            <th>标题</th>
                            <th>缩略图</th>
                            <th>所属分类</th>
                            <th>排序</th>
                            <th>发布日期</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach($ads as $ad)
                            <tr class="odd" data-id="{{$ad->id}}">
                                <td class="center">
                                    <label>
                                        <input type="checkbox" class="ace checked_id" name="checked_id[]"
                                               value="{{$ad->id}}">
                                        <span class="lbl"></span>
                                    </label>
                                </td>
                                <td>{{$ad->id}}</td>
                                <td>{{$ad->title}}</td>
                                <td><img src="{{$ad->image}}" style="width: 50px;height: 35px;"></td>
                                <td>{{$ad->category->name}}</td>
                                <td>
                                    <input type="text" class="ace sort_order" value="{{$ad->sort_order}}"
                                           style="width: 40px;">
                                </td>
                                <td>{{$ad->created_at}}</td>
                                <td>
                                    <div class="visible-md visible-lg hidden-sm hidden-xs action-buttons">
                                        <a class="green" href="{{route('ads.ad.edit', $ad->id)}}"><i
                                                    class="icon-pencil bigger-130"></i></a>
                                        <a class="red" href="{{route('ads.ad.destroy', $ad->id)}}"
                                           data-method="delete" data-token="{{csrf_token()}}"
                                           data-confirm="确定要删除此条记录么?">
                                            <i class="icon-trash bigger-130"></i></a>
                                    </div>
                                </td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                    <div class="row">
                        <div class="col-sm-6">共 {{$ads->total()}} 条记录</div>
                        <div class="col-sm-6">
                            <div class="dataTables_paginate paging_bootstrap">
                                <ul class="pagination">
                                    {{ $ads->appends(Request::all())->links() }}
                                </ul>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
@endsection

@section('js')
    <script>
        $(function () {
            //ajax排序
            $('.sort_order').change(function () {
                var info = {
                    id: $(this).parents('tr').data('id'),
                    sort_order: $(this).val()
                };

                $.ajax({
                    type: 'PATCH',
                    url: '{{route('ads.ad.sort_order')}}',
                    data: info,
                    success: function (data) {
                        window.location.reload();
                    }
                })
            });


            //全选，反选
            $("#check_all").click(function () {
                $(':checkbox').prop("checked", this.checked);
            });


            //删除多条
            $('.del_all').click(function () {
                var length = $(".checked_id:checked").length;
                if (length == 0) {
                    alert('您至少要选中一条记录~');
                    return false;
                }

                var checked_id = $(".checked_id:checked").serialize();

                $.ajax({
                    type: 'DELETE',
                    url: '{{route('ads.ad.destroy_checked')}}',
                    data: checked_id,
                    success: function () {
                        location.href = location.href;
                    }
                })
            });

            //按分类搜索
            $("#change_category").change(function () {
                var category_id = $(this).val();
                if (category_id == "-1") {
                    location.href = "{{route('ads.ad.index')}}";
                    return false;
                }
                var url = "/admin/ads/ad?category_id=" + category_id;
//                console.log(url);return false;
                location.href = url;
            })
        });
    </script>
@endsection

